//
//  MbRewardAd.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbBannerAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>
#import "GDTUnifiedBannerView.h"


@interface MbBannerAd()<BUNativeExpressBannerViewDelegate,GDTUnifiedBannerViewDelegate,ABUBannerAdDelegate>

@property(nonatomic,strong)BUNativeExpressBannerView *ttBannerAd;

@property(nonatomic,strong)GDTUnifiedBannerView *gdtBannerAd;
@property(nonatomic,strong)ABUBannerAd *gromoreAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end

@implementation MbBannerAd


-(void)loadBanner{
    if(!self.adItems || self.adItems.count == self.index  || !self.rootVC ){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(bannerAdDidError:)]) {
            [self.delegate bannerAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        self.ttBannerAd = [[BUNativeExpressBannerView alloc] initWithSlotID:adItem.codeNo rootViewController:self.rootVC
                                                                     adSize:CGSizeMake(self.width, self.height)];
        self.ttBannerAd.frame = CGRectMake(0, 0, self.width, self.height);
        self.ttBannerAd.delegate = self;
        [self.ttBannerAd loadAdData];
        

    } else if(adItem.plat == GDT){
        self.gdtBannerAd = [[GDTUnifiedBannerView alloc]
                               initWithFrame:CGRectMake(0, 0, self.width, self.height)
                               placementId:adItem.codeNo
                               viewController:self.rootVC];
        self.gdtBannerAd.animated = YES;
        self.gdtBannerAd.delegate = self;
        [self.gdtBannerAd loadAdAndShow];
        [self.view addSubview:self.gdtBannerAd];
        
    } else if(adItem.plat == KS){

    } else if(adItem.plat == GroMore){
       
        self.gromoreAd = [[ABUBannerAd alloc] initWithAdUnitID:adItem.codeNo rootViewController:self.rootVC adSize:CGSizeMake(self.width, self.height)];
        self.gromoreAd.delegate = self;
        //ABUD_Log2(@"%s",__func__);
        //该逻辑用于判断配置是否拉取成功。如果拉取成功，可直接加载广告，否则需要调用setConfigSuccessCallback，传入block并在block中调用加载广告。SDK内部会在配置拉取成功后调用传入的block
        __weak typeof(self) weakself = self;
        //当前配置拉取成功，直接loadAdData
        if([ABUAdSDKManager configDidLoad]){
            //ABUD_Log2(@"%s:---当前配置拉取成功，直接加载广告",__func__);
            [self.gromoreAd loadAdData];
        }else{
            //当前配置未拉取成功，在成功之后会调用该callback
            //ABUD_Log2(@"%s:---配置拉取未成功，延后加载广告",__func__);
            [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(id  _Nonnull observer) {
                //ABUD_Log(@"%s: ----setConfigSuccessCallback", __func__);
                [weakself.gromoreAd loadAdData];
            }];
        }
    }
    self.index ++;
}


#pragma mark 穿山甲回调
- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView{
    if(self.view){
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                     self.height);
        [self.view addSubview:bannerAdView];
    }
    
    if ([self.delegate respondsToSelector:@selector(bannerAdDidLoad:)]) {
        [self.delegate bannerAdDidLoad:bannerAdView];
    }
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterwords{
    if(self.view){
        [self.view removeFromSuperview];
    }
    if ([self.delegate respondsToSelector:@selector(bannerAdDislike:)]) {
        [self.delegate bannerAdDislike:bannerAdView];
    }
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *)error{
    self.error = error;
    [self loadBanner];
}
#pragma mark 优量汇回调
- (void)unifiedBannerViewDidLoad:(GDTUnifiedBannerView *)unifiedBannerView{
    if(self.view){
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                     self.height);
        [self.view addSubview:unifiedBannerView];
    }
    if ([self.delegate respondsToSelector:@selector(bannerAdDidLoad:)]) {
        [self.delegate bannerAdDidLoad:unifiedBannerView];
    }
}

- (void)unifiedBannerViewWillClose:(GDTUnifiedBannerView *)unifiedBannerView{
    if(self.view){
        [self.view removeFromSuperview];
    }
    if ([self.delegate respondsToSelector:@selector(bannerAdDislike:)]) {
        [self.delegate bannerAdDislike:unifiedBannerView];
    }
}

- (void)unifiedBannerViewFailedToLoad:(GDTUnifiedBannerView *)unifiedBannerView error:(NSError *)error{
    self.error = error;
    [self loadBanner];
}

#pragma mark 快手回调

#pragma mark gromore回调

- (void)bannerAdDidLoad:(ABUBannerAd *)bannerAd bannerView:(UIView *)bannerView{
    if(self.view){
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.width,
                                     self.height);
        [self.view addSubview:bannerView];
    }
    
    if ([self.delegate respondsToSelector:@selector(bannerAdDidLoad:)]) {
        [self.delegate bannerAdDidLoad:bannerView];
    }
}
- (void)bannerAdDidClosed:(ABUBannerAd *)ABUBannerAd bannerView:(UIView *)bannerView dislikeWithReason:(NSArray<NSDictionary *> *)filterwords{
    if(self.view){
        [self.view removeFromSuperview];
    }
    if ([self.delegate respondsToSelector:@selector(bannerAdDislike:)]) {
        [self.delegate bannerAdDislike:bannerView];
    }
}

- (void)bannerAd:(ABUBannerAd *)bannerAd didLoadFailWithError:(NSError *)error{
    self.error = error;
    [self loadBanner];
}
@end
